//
//  TextFieldCanMoceView.m
//  BigJoy
//
//  Created by Rowling on 2017/6/22.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "TextFieldCanMoceView.h"
@interface TextFieldCanMoceView()<UITextViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic,assign) CGFloat fontSize;
@property (nonatomic,assign) BOOL isDelete;
@end

@implementation TextFieldCanMoceView

-(UITextField *)textField {
    if (_textField == nil) {
        _textField = [[UITextField alloc]init];
    }
    return _textField;
}


-(UITextView *)XMtextView {
    if (_XMtextView == nil) {
        _XMtextView = [[UITextView alloc]init];
        self.fontSize = 100;
    }
    return _XMtextView;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UIButton class]]) {
        return NO;
    }else {
        return YES;
    }
}

-(UITapGestureRecognizer *)doubleTap {
    if (_doubleTap == nil) {
        _doubleTap = [[UITapGestureRecognizer alloc]init];
        [_doubleTap setNumberOfTapsRequired:2];
        [self addGestureRecognizer:_doubleTap];
        [_doubleTap addTarget:self action:@selector(doubtap)];
        _doubleTap.delegate = self;
        [self.tap requireGestureRecognizerToFail:_doubleTap];
    }
    return _doubleTap;
}

// 双击的方法
-(void)doubtap {
    NSLog(@"douclikc");
    [self.Delegate XMdoublClick:self];
}

-(void)setUI {
    [super setUI];
    [self doubleTap];
    if (self.fontName == nil) {
        self.fontName = @"PingFang SC";
    }
    self.XMtextView.frame = CGRectMake(1, 1 + Inset, self.bounds.size.width - 2 - Inset, self.bounds.size.height - 2 - Inset);
    self.XMtextView.font = [UIFont fontWithName:self.fontName size:self.fontSize];
    self.XMtextView.textAlignment = 1;
    self.XMtextView.delegate = self;
    self.XMtextView.autocorrectionType = UITextAutocorrectionTypeNo;
    self.XMtextView.spellCheckingType = UITextSpellCheckingTypeNo;
    self.XMtextView.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.XMtextView.backgroundColor = [UIColor clearColor];
    self.XMtextView.bounces = NO;
    [self addSubview:self.XMtextView];
}

-(void)textViewDidChange:(UITextView *)textView {
    NSLog(@"--%@--",textView.text);
     NSArray * array = [textView.text componentsSeparatedByString:@"\n"];
    NSLog(@"%ld",array.count);
    CGFloat i = array.count;
    CGFloat maxH = textView.frame.size.height;
    
    CGSize tt = [textView.text boundingRectWithSize:CGSizeMake(textView.frame.size.width - self.fontSize,MAXFLOAT)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName: [UIFont fontWithName:self.fontName size:self.fontSize]} context:nil].size;
    
    NSLog(@"%@",NSStringFromCGSize(tt));
    CGFloat limitH = maxH ;
    CGFloat superH = maxH - 20;
    if (tt.height < maxH - 20 && i > 1) {
        limitH = tt.height;
    }else {
        superH = tt.height;
    }
    
    if (self.isDelete) {
        CGSize tH = [textView.text boundingRectWithSize:CGSizeMake(MAXFLOAT,limitH)
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName: [UIFont fontWithName:self.fontName size:self.fontSize]} context:nil].size;
        
        if (superH >= maxH - 20) {
        
        }else {
            while( tH.width < textView.frame.size.width - self.fontSize && self.fontSize < 100) {
                self.fontSize++;
                tH = [textView.text boundingRectWithSize:CGSizeMake(MAXFLOAT,limitH)
                                                 options:NSStringDrawingUsesLineFragmentOrigin
                                              attributes:@{NSFontAttributeName: [UIFont fontWithName:self.fontName size:self.fontSize]} context:nil].size;
            }
        }
       
        self.XMtextView.font = [UIFont fontWithName:self.fontName size:self.fontSize];
    }else{
        CGSize tH = [textView.text boundingRectWithSize:CGSizeMake(MAXFLOAT,limitH)
                               options:NSStringDrawingUsesLineFragmentOrigin
                            attributes:@{NSFontAttributeName: [UIFont fontWithName:self.fontName size:self.fontSize]} context:nil].size;
        while((tH.width > textView.frame.size.width - self.fontSize && self.fontSize > 0) || superH > maxH - 20 ) {
            self.fontSize--;
            tH = [textView.text boundingRectWithSize:CGSizeMake(MAXFLOAT,limitH)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName: [UIFont fontWithName:self.fontName size:self.fontSize]} context:nil].size;
            
            tt = [textView.text boundingRectWithSize:CGSizeMake(self.XMtextView.size.width - self.fontSize,MAXFLOAT)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName: [UIFont fontWithName:self.fontName size:self.fontSize]} context:nil].size;
            superH = tt.height;
        }
        self.XMtextView.font = [UIFont fontWithName:self.fontName size:self.fontSize];
    }
}



-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@""]) {
        self.isDelete = YES;
    } else {
        self.isDelete = NO;
    }
    return YES;
}



-(void)setIsEditing:(BOOL)isEditing {
    _isEditing = isEditing;
    self.XMtextView.userInteractionEnabled = isEditing;
    if (isEditing) {
        [self.XMtextView becomeFirstResponder];
    } else {
        [self.XMtextView endEditing:YES];
    }
}

-(void)setAlengment:(NSInteger)alengment {
    _alengment = alengment;
    self.XMtextView.textAlignment = _alengment;
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    
    return self.isEditing;
}

-(void)setFontName:(NSString *)fontName {
    _fontName = fontName;
    self.XMtextView.font = [UIFont fontWithName:fontName size:self.fontSize];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return YES;
}



@end
